package com.tigger.core.redisson;

import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;

import java.util.concurrent.TimeUnit;

/**
 * Created by root on 2018/7/21.
 */
public class RedissonDistributedReadWriteLocker implements DistributedRReadWriteLocker {

    private RedissonClient redissonClient;

    @Override
    public boolean readLock(String lockKey) {
        RReadWriteLock rrwLock = redissonClient.getReadWriteLock(lockKey);
        RLock lock = rrwLock.readLock();
        if(!lock.isLocked()) {
            lock.lock();
            return lock.isLocked();
        }
        return false;
    }

    @Override
    public boolean writeLock(String lockKey) {
        RReadWriteLock rrwLock = redissonClient.getReadWriteLock(lockKey);
        RLock lock = rrwLock.writeLock();
        if(!lock.isLocked()) {
            lock.lock();
            return lock.isLocked();
        }
        return false;
    }

    @Override
    public boolean readLock(String lockKey, TimeUnit unit, int timeout) {
        RReadWriteLock rrwLock = redissonClient.getReadWriteLock(lockKey);
        RLock lock = rrwLock.readLock();

        if(!lock.isLocked()) {
            lock.lock(timeout, unit);
            return lock.isLocked();
        }
        return false;
    }

    @Override
    public boolean writeLock(String lockKey, TimeUnit unit, int timeout) {
        RReadWriteLock rrwLock = redissonClient.getReadWriteLock(lockKey);
        RLock lock = rrwLock.writeLock();
        if(!lock.isLocked()) {
            lock.lock(timeout, unit);
            return lock.isLocked();
        }
        return false;
    }

    @Override
    public boolean tryReadLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {
        RReadWriteLock rrwLock = redissonClient.getReadWriteLock(lockKey);
        RLock lock = rrwLock.readLock();
        try {
            return tryLock(lock, unit, waitTime, leaseTime);
        } catch (InterruptedException ex) {
            return false;
        }

    }

    @Override
    public boolean tryWriteLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {
        RReadWriteLock rrwLock = redissonClient.getReadWriteLock(lockKey);
        RLock lock = rrwLock.writeLock();
        try {
            return tryLock(lock, unit, waitTime, leaseTime);
        } catch (InterruptedException ex) {
            return false;
        }
    }

    private boolean tryLock(RLock lock, TimeUnit unit, int waitTime, int leaseTime) throws InterruptedException {
        if(!lock.isLocked()) {
            return lock.tryLock(waitTime, leaseTime, unit);
        }
        return false;
    }

    @Override
    public void unlock(String lockKey) {
        RReadWriteLock rrwLock = redissonClient.getReadWriteLock(lockKey);
        RLock lock = rrwLock.readLock();
        lock.unlock();
        lock = rrwLock.writeLock();
        lock.unlock();
    }

    @Override
    public void unlock(RLock lock) {
        lock.unlock();
    }

    @Override
    public void releaseReadLock(String lockKey) {
        RReadWriteLock rrwLock = redissonClient.getReadWriteLock(lockKey);
        RLock lock = rrwLock.readLock();
        lock.unlock();
    }

    @Override
    public void releaseWriteLock(String lockKey) {
        RReadWriteLock rrwLock = redissonClient.getReadWriteLock(lockKey);
        RLock lock = rrwLock.writeLock();
        lock.unlock();
    }

    public void setRedissonClient(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }
}
